//
//  WJTopicPictureView.m
//  BeDeJie
//
//  Created by jerry on 2017/2/2.
//  Copyright © 2017年 jerry. All rights reserved.
//

#import "WJTopicPictureView.h"
#import <UIImageView+WebCache.h>
#import "WJSeeBigPictureViewController.h"

@interface WJTopicPictureView()

@property (weak, nonatomic) IBOutlet UIImageView *bgImageView;
@property (weak, nonatomic) IBOutlet UIImageView *gifView;
@property (weak, nonatomic) IBOutlet UIButton *seeBigPictureButton;

@end

@implementation WJTopicPictureView

- (void)awakeFromNib{
    [super awakeFromNib];
    
    //取消Autoresizing
    self.autoresizingMask = UIViewAutoresizingNone;
    //背景图片可以与用户交互
    self.bgImageView.userInteractionEnabled = YES;
    //背景图片设置点击后打开查看大图页面
    [self.bgImageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(seeBigPicture)]];
}

// 查看大图
- (void)seeBigPicture {
    WJSeeBigPictureViewController *seeBigPictureViewController = [[WJSeeBigPictureViewController alloc] init];
    seeBigPictureViewController.topic = self.topic;
    
    //第一种获取根控制器的方式
    [self.window.rootViewController presentViewController:seeBigPictureViewController animated:YES completion:nil];
    //第二种获取根控制器的方式
//    [UIApplication sharedApplication].keyWindow.rootViewController;
}


- (void)setTopic:(WJTopic *)topic {
    _topic = topic;

    // 设置图片
    [self.bgImageView setOriginImage:topic.image1 thumbnailImage:topic.image0 placeholder:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        if (!image) return;
        
        // 处理超长图片的大小
        if (topic.bigPicture) {
            /*
             middleFrame.width     topic.width
             ------------------ == -----------
             middleFrame.height    topic.height
             
             self.width * middleH == middleW * self.height
             */
            CGFloat imageW = topic.middleFrame.size.width;
            CGFloat imageH = imageW * topic.height / topic.width;
            
            // 开启上下文
            UIGraphicsBeginImageContext(CGSizeMake(imageW, imageH));
            // 绘制图片到上下文中
            [self.bgImageView.image drawInRect:CGRectMake(0, 0, imageW, imageH)];
            self.bgImageView.image = UIGraphicsGetImageFromCurrentImageContext();
            // 关闭上下文
            UIGraphicsEndImageContext();
        }
    }];
    
    // gif
    self.gifView.hidden = !topic.is_gif;  //如果不是gif图，则隐藏gif图标
    // 判断是否是gif图的方法
    // 方法一：[topic.image1.lowercaseString hasSuffix:@"gif"]
    // 方法二：[topic.image1.pathExtension.lowercaseString isEqualToString:@"gif"]
    
    // 点击查看大图
    if (topic.bigPicture) { // 超长图
        self.seeBigPictureButton.hidden = NO;
        self.bgImageView.contentMode = UIViewContentModeTop;
        self.bgImageView.clipsToBounds = YES;
    } else {
        self.seeBigPictureButton.hidden = YES;
        self.bgImageView.contentMode = UIViewContentModeScaleToFill;
        self.bgImageView.clipsToBounds = NO;
    }
    
}


@end
